package c.e.a.a.f.p.i;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.util.Base64;
import c.e.a.a.a;
import c.e.a.a.f.e;
import c.e.a.a.f.h;
import c.e.a.a.f.p.i.t.b;
import com.huawei.hms.jos.games.ranking.RankingConst;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map.Entry;

/* compiled from: SQLiteEventStore */
public final /* synthetic */ class s implements b {
    public final t a;
    public final h b;
    public final e c;

    public s(t tVar, h hVar, e eVar) {
        this.a = tVar;
        this.b = hVar;
        this.c = eVar;
    }

    public Object a(Object obj) {
        t tVar = this.a;
        h hVar = this.b;
        e eVar = this.c;
        SQLiteDatabase sQLiteDatabase = (SQLiteDatabase) obj;
        a aVar = t.e;
        if ((tVar.n().compileStatement("PRAGMA page_size").simpleQueryForLong() * tVar.n().compileStatement("PRAGMA page_count").simpleQueryForLong() >= tVar.d.e() ? 1 : null) != null) {
            return Long.valueOf(-1);
        }
        long longValue;
        Long o = tVar.o(sQLiteDatabase, hVar);
        if (o != null) {
            longValue = o.longValue();
        } else {
            ContentValues contentValues = new ContentValues();
            contentValues.put("backend_name", hVar.b());
            contentValues.put("priority", Integer.valueOf(c.e.a.a.f.s.a.a(hVar.d())));
            contentValues.put("next_request_ms", Integer.valueOf(0));
            if (hVar.c() != null) {
                contentValues.put("extras", Base64.encodeToString(hVar.c(), 0));
            }
            longValue = sQLiteDatabase.insert("transport_contexts", null, contentValues);
        }
        int d = tVar.d.d();
        byte[] bArr = eVar.d().b;
        boolean z = bArr.length <= d;
        ContentValues contentValues2 = new ContentValues();
        contentValues2.put("context_id", Long.valueOf(longValue));
        contentValues2.put("transport_name", eVar.g());
        contentValues2.put("timestamp_ms", Long.valueOf(eVar.e()));
        contentValues2.put("uptime_ms", Long.valueOf(eVar.h()));
        contentValues2.put("payload_encoding", eVar.d().a.a);
        contentValues2.put("code", eVar.c());
        contentValues2.put("num_attempts", Integer.valueOf(0));
        contentValues2.put("inline", Boolean.valueOf(z));
        contentValues2.put("payload", z ? bArr : new byte[0]);
        longValue = sQLiteDatabase.insert("events", null, contentValues2);
        String str = "event_id";
        if (!z) {
            int ceil = (int) Math.ceil(((double) bArr.length) / ((double) d));
            for (int i = 1; i <= ceil; i++) {
                byte[] copyOfRange = Arrays.copyOfRange(bArr, (i - 1) * d, Math.min(i * d, bArr.length));
                ContentValues contentValues3 = new ContentValues();
                contentValues3.put(str, Long.valueOf(longValue));
                contentValues3.put("sequence_num", Integer.valueOf(i));
                contentValues3.put("bytes", copyOfRange);
                sQLiteDatabase.insert("event_payloads", null, contentValues3);
            }
        }
        for (Entry entry : Collections.unmodifiableMap(eVar.b()).entrySet()) {
            ContentValues contentValues4 = new ContentValues();
            contentValues4.put(str, Long.valueOf(longValue));
            contentValues4.put(RankingConst.RANKING_JGW_NAME, (String) entry.getKey());
            contentValues4.put("value", (String) entry.getValue());
            sQLiteDatabase.insert("event_metadata", null, contentValues4);
        }
        return Long.valueOf(longValue);
    }
}
